import os
import math
from django import template

register = template.Library()

@register.filter
def upper_folder(path):
    splits = os.path.split(path)
    return splits[0] if len(splits) > 0 else path


@register.filter
def convert_size(size):
    per = ["B","K","M","G","T"]
    idx = 0
    for i in range(len(per)):
        tmp = size / 1024
        if int(tmp) < 1:
            break
        size = tmp
        idx = i

    return f"{size:.2f}{per[idx]}"

